import { parse } from 'url';
import webpath from '@/utils/webpath';
// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
        user_name: `张三 menu_id ${i}`,
        user_name_cn: `张三 auth_id ${i}`,
        org_code: '2019-09-08',
    });
}
let tableMenu = [];
	tableMenu.push(
	  { title: '学校银行', key: '00000000',dept_code:"0000" },
      { title: '姜旭聪你想什么呢', key:'6',dept_code:"0000",dept_name:'曹耀伟',dept_id:2341 },
      { title: '电脑电脑', key: '7', isLeaf: true,dept_code:"0000" },
	);
let treeNodeArray = [
'0','1','2',
];
let treeNode = 2;
function getAllDeptTreeByUserId (req,res,u,b) {
	treeNode += 1;
	treeNodeArray = [...treeNodeArray,treeNode];
	let result = {
        list: treeNodeArray,
    };
	return res.json(result);
}
function getMenuTree(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableMenu;
    const result = {
        list: dataSource,
    };
    return res.json(result);
}

function getProDeptTreeByLevelCode (req,res,u,b){
	return getMenuTree(req, res, u)
}

function getProDeptTreeByParentid (req,res,u,b){
	return getMenuTree(req, res, u)
}

function getProUserDept(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableListDataSource;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}

function deleteByPrimaryKeyForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);

    return getProUserDept(req, res, u);
}


function selectByPrimaryKeyForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);

return getProUserDept(req, res, u);

}



function insertForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;



    const i = Math.ceil(Math.random() * 10000);
    tableListDataSource.unshift({
        menu_id: `李海波 menu_id ${i}`,
        auth_id: `李海波 auth_id ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });

    return getProUserDept(req, res, u);
}


function updateForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.map(item => {
        if (item.id === id) {
        Object.assign(item, { desc, name });
        return item;
    }
    return item;
});

    return getProUserDept(req, res, u);
}


function deleteByMapForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);

return getProUserDept(req, res, u);
}


function updateByMapForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);

return getProUserDept(req, res, u);
}

function getOneForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(undefined!=id)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);


    return getProUserDept(req, res, u);
}

function getAllForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);


    return getProUserDept(req, res, u);
}

function queryPageForProUserDept(req, res, u, b) {


    return getProUserDept(req, res, u);
}
function getPageProUserByDeptCode(req, res, u, b) {


    return getProUserDept(req, res, u);
}

function countForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);

return getProUserDept(req, res, u);
}


function insertBatchForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);

return getProUserDept(req, res, u);
}


function deleteBatchForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);

return getProUserDept(req, res, u);
}


function updateBatchForProUserDept(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);

return getProUserDept(req, res, u);
}




export default {
    'GET /${webpath}/web/api/service/prouserprodept/get': selectByPrimaryKeyForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/insert': insertForProUserDept,
    'GET /${webpath}/web/api/service/prouserprodept/delete': deleteByPrimaryKeyForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/update': updateForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/deleteByMap': deleteByMapForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/updateByMap': updateByMapForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/getOne': getOneForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/getAll': getAllForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/queryPage': queryPageForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/count': countForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/insertBatch': insertBatchForProUserDept,
    'POST /${webpath}/web/api/service/prouserprodept/deleteBatch': deleteBatchForProUserDept,
    
    'POST /${webpath}/web/api/service/prodept/getProDeptTreeByLevelCode': getProDeptTreeByLevelCode,
    'POST /${webpath}/web/api/service/prodept/getPageProUserByDeptCode': getPageProUserByDeptCode,
    'POST /${webpath}/web/api/service/prodept/getProDeptTreeByParentid': getProDeptTreeByParentid,
    
    'GET /${webpath}/web/api/service/prodept/delete': deleteByPrimaryKeyForProUserDept,
    'POST /${webpath}/web/api/service/prodept/insert': insertForProUserDept,
    'POST /${webpath}/web/api/service/prodept/update': updateForProUserDept,
    
    'POST /${webpath}/web/api/service/prouser/queryPage': queryPageForProUserDept,
};
